{% extends "swarming/base.html" %}


{% block headers %}
<style>
  h1 {
    margin-top: 10px;
    margin-bottom: 10px;
  }

  h3 {
    margin-bottom: 5px;
  }

  table.property_table {
    font-family: monospace;
    border-spacing: 0;
  }

  table.property_table tbody tr:nth-child(even) {
    background-color: #eeeeee;
  }

  table.property_table td {
    padding-left: 1em;
    padding-right: 1em;
  }

  table.property_table tbody tr:hover {
    background-color: #eeffee;
  }

  .bad_version {
    background-color: #ffffee;
  }

  .dead_bot {
    background-color: #ffdddd;
  }
</style>
{% endblock %}


{% block body %}
{% import 'swarming/bot_view.html' as bot_view %}
<h1>Bot details</h1>
<a href="/restricted/bots">Back to bots list</a>
<p>

{% if bot %}
  <table class="property_table">
    <tbody>
      <tr title="The bot id is what uniquely identify the bot.">
        <td>ID</td>
        <td>{{bot.id}}</td>
      </tr>
      <tr title="IP address that the server saw the connection from.">
        <td>External IP</td>
        <td>{{bot.external_ip}}</td>
      </tr>
      <tr title="First time ever the bot contacted the server.">
        <td>First seen</td>
        <td>{{bot.first_seen_ts|datetimeformat}}</td>
      </tr>
      <tr title="Last time the bot contacted the server.">
        <td>Last contact</td>
        <td nowrap {% if bot.is_dead(now) %}class="dead_bot"{% endif %}>
          {{(now-bot.last_seen_ts)|timedeltaformat}} ago
          {% if is_admin and bot.is_dead(now) %}
          &nbsp;&nbsp;
          <form id="form_delete" method="post"
              action="/restricted/bot/{{bot.id}}/delete">
            <input type="hidden" name="xsrf_token" value="{{xsrf_token}}" />
            <input type="submit" value="Delete" />
          </form>
          {% endif %}
        </td>
      </tr>
      <tr title="Bot's dimension is the list of properties that the requests dimensions are matched against. If all the requests dimensions are in the bot dimensions, the bot is allowed to execute the task.">
        <td>Dimensions</td>
        <td>{{bot_view.bot_dimensions(bot)}}</td>
      </tr>
      <tr title="Bot's state is informative only.">
        <td>State</td>
        <td>{{bot_view.render_dict(bot.state)}}</td>
      </tr>
      <tr title="Version is based on the content of swarming_bot.zip which is the swarming bot code.">
        <td>Version</td>
        <td>{{bot.version[:8]}}</td>
      </tr>
      <tr title="The version the server expects the bot to be using.">
        <td>Expected version</td>
        <td>{{current_version[:8]}}</td>
      </tr>
      <tr title="Current task handled by the bot, if any.">
        <td>Current task</td>
        <td>
          {% if bot.task %}
            <a href="/user/task/{{bot.task_id}}">{{bot.task_name}}</a>
          {% else %}
            &#8209;&#8209;
          {% endif %}
        </td>
      </tr>
      <tr title="Sum of duration of all the tasks listed below.">
        <td>Total running time<br>for past {{ run_results|length }} tasks</td>
        <td>{{run_time|timedeltaformat}}</td>
      </tr>
      <tr title="Gaps between all the tasks listed below where the bot didn't execute a task. The bot could be rebooting during this time." >
        <td>Total idle time</td>
        <td>{{idle_time|timedeltaformat}}</td>
      </tr>
    </tbody>
  </table>
  <p>

  {% if events %}
    {# TODO(maruel): Merge events and tasks together. #}
    <h3>Recent events</h3>
    <table class="property_table">
      <thead>
        <tr>
          <th>Timestamp</th>
          <th>Type</th>
          <th>Quarantined</th>
          <th>Task</th>
          <th>Msg</th>
          <th>Version</th>
        </tr>
      </thead>
      <tbody>
        {% for event in events %}
        {# TODO(maruel): Create hover text that includes all the data (including
        dimensions and state about the event #}
        <tr>
          <td>{{event.ts|succinctdatetimeformat}}</td>
          <td>{{event.event_type}}</td>
          <td>
            {% if event.quarantined %}{{event.quarantined}}{% endif %}
          </td>
          <td>
            {% if event.task_id %}
              <a href="/user/task/{{event.task_id}}">{{event.task_id}}</a>
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td>
            {% if event.message %}
              <pre>{{event.message}}</pre>
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td {%if event.version != current_version%}class="bad_version"{%endif%}>
            {{event.version[:8]}}
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}

  {% if run_results %}
    <h3>Previous tasks</h3>
    <table class="property_table">
      <thead>
        <tr>
          <th>Task</th>
          <th>Started</th>
          <th>Duration</th>
          <th>Idle time after</th>
          <th>Result</th>
        </tr>
      </thead>
      <tbody>
        {% set previous = false %}
        {% for run_result in run_results %}
          <tr class="request {% if run_result.failure %}failed_test{% endif%}">
            <td>
              <a href="/user/task/{{run_result.key_packed}}">
                {{run_result.name}}</a>
            </td>
            <td nowrap>{{run_result.started_ts|succinctdatetimeformat}}</td>
            <td nowrap align=right>{{run_result.duration_now(now)|timedeltaformat}}</td>
            <td nowrap align=right>
              {% if previous and previous.started_ts and run_result.ended_ts %}
                {# Note that tasks are listed in reverse order #}
                {{(previous.started_ts - run_result.ended_ts)|timedeltaformat}}
              {% elif run_result.ended_ts %}
                {% if not cursor %}
                  {{(now - run_result.ended_ts)|timedeltaformat}}
                {% else %}
                  &#8209;&#8209;
                {% endif %}
              {% else %}
                (Still running)
              {% endif %}
            </td>
            <td>{{run_result.to_string()}}</td>
          </tr>
          {% set previous = run_result %}
        {% endfor %}
      </tbody>
    </table>
  {% endif %}

  {% if cursor %}
  <br>
  <a href="/restricted/bot/{{bot.id}}?limit={{limit}}&cursor={{cursor}}"}>
    Next page</a>
  {% endif %}

{% else %}

  Bot {{bot_id}} not found.

{% endif %}

{% endblock %}
